package Base.ProjectTest01.Service;

import Base.ProjectTest01.Domain.Architect;
import Base.ProjectTest01.Domain.Designer;
import Base.ProjectTest01.Domain.Employee;
import Base.ProjectTest01.Domain.Programmer;

/**
 * @author 枫城
 * @createTime 2022/10/19 18:30
 * @desc 关于开发团队成员的管理:添加、删除等。
 */
public class TeamService {

    private static int counter = 1; // 给memberID赋值使用
    private final int MAX_MEMBER = 5;
    private Programmer[] team = new Programmer[MAX_MEMBER]; // 保存开发成员
    private int total; // 记录实际开发人数

    public TeamService() {
    }

    /**
     * @author 枫城
     * @createTime 2022/10/19 22:00
     * @desc 获取开发所有成员
     * @param
     * @return 团队列表
     */
    public Programmer[] getTeam(){

        Programmer[] team = new Programmer[total];
        for (int i = 0; i < team.length; i++) {
            team[i] = this.team[i];
        }
        return team;
    }

    /**
     * @author 枫城
     * @createTime 2022/10/19 22:03
     * @desc 将指定员工添加到指定团队中
     * @param employee
     */
    public void addMember(Employee employee) throws TeamException {
//        成员已满，无法添加
//        该成员不是开发人员，无法添加
//        该员工已在本开发团队中
//        该员工已是某团队成员
//        该员正在休假，无法添加
        if (total>=MAX_MEMBER){
            throw new TeamException("成员已满，无法添加");
        }
        if (!(employee instanceof Programmer)){
            throw new TeamException(" 该成员不是开发人员，无法添加");
        }
        if (isExit(employee)){
            throw new TeamException("该员工已在本开发团队中");
        }
        if (((Programmer) employee).getStatus().getNAME().equals("BUSY")){
            throw new TeamException("该员工已是某团队成员");
        }else if (((Programmer) employee).getStatus().getNAME().equals("VOCATION")){
            throw new TeamException("该员正在休假，无法添加");
        }

//        团队中至多只能有-名架构师
//        团队中至多只能有两名设计师
//        团队中至多只能有三名程序员
        int numOfArch =0, numOfDes=0, numOfPro=0;
        for (int i = 0; i < total; i++) {
            if (team[i] instanceof Architect){
                numOfArch++;
            }else if(team[i] instanceof Designer){
                numOfDes++;
            }else if (team[i] != null){
                numOfPro++;
            }
        }
        if (employee instanceof Architect){
            if (numOfArch >= 1){
                throw new TeamException("团队中至多只能有-名架构师");
            }
        }
        if (employee instanceof Designer){
            if (numOfDes >= 2 ){
                throw new TeamException("团队中至多只能有两名设计师");
            }
        }
        if (numOfPro >= 3){
            throw new TeamException(" 团队中至多只能有三名程序员");
        }

        // 添加操作
        team[total++] = (Programmer) employee;
        // 修改属性
        ((Programmer) employee).setStatus(Status.BUSY);
        ((Programmer) employee).setMemberId(counter++);


    }

    /**
     * @author 枫城
     * @createTime 2022/10/20 16:59
     * @desc 通过TeamID删除团队人员
     * @param memberId
     */
    public void removeMember(int memberId) throws TeamException {
        int i = 0;
        for (; i < total; i++) {
            if (team[i].getMemberId() == memberId){
                team[i].setStatus(Status.FREE);
                break;
            }
        }
        if (i == total){
            throw new TeamException("未找到该员工");
        }

        for (int j = i+1;j<total;){
            team[j-1] = team[j];
        }
        team[--total] = null;
    }


    /**
     * @author 枫城
     * @createTime 2022/10/19 22:17
     * @desc 判断该员工是否在团队中
     * @param employee
     * @return true or false
     */
    private Boolean isExit(Employee employee){
        for (int i = 0; i < total; i++) {
            if (team[i].getMemberId()  == employee.getId() ){
                return true;
            }
        }
        return false;
    }


}
